import React from 'react'
import { changeNav } from '../../../actions'
import { connect } from 'react-redux'
import {
    Button, List, InputItem, Flex
    , WingBlank, WhiteSpace, Popup, Tag, Toast, Icon,Switch
} from 'antd-mobile'
import hhistory from '../../../history'
import { createForm } from 'rc-form'
import { ajaxGet, ajaxPost } from 'wxAjaxUtil';

const Item = List.Item;
const Brief = Item.Brief;

class WxmtPutInfo extends React.Component{
    constructor(props) {
        super(props);
        this.state = {
            info:{}
        }
    }
    componentDidMount(){
        this.props.tochangeNav({title:"美团外卖配置",rbtn:[
            <Button key="1" onClick={() => {
                this.props.form.validateFields(( error,value )=> {
                    if (error) {
                        Toast.info('请填写必填项')
                        return
                    }
                    let data = {
                        storeInfo : [this.props.location.state.code],
                        username: value.name,
                        password:value.password,
                        riderFlg:value.riderFlg?1:0
                    }
                    ajaxPost('o2o/sly/conf/apply/mtwm/shop',data,'wxApiPath').then(v => {
                        if(v.errorCode == '200'){
                            Toast.info('修改成功',1.5,() => {
                                hhistory.goBack();
                            })
                        }else{
                            Toast.info(v.errorText,1.5)
                        }
                    })
                })
            }} type="primary" inline size="small">确定</Button>
        ]});
        this.getstoreInfo(this.props.location.state.code);
    }

    getstoreInfo(code) {
        let storeCode = code
        let that = this;
        ajaxPost('o2o/sly/conf/load/mtwm/shop',{storeCode},'wxApiPath').then( v => {
            if(v.errorCode == '200'){
                that.setState({
                    info:v.data
                })
            }else{
                Toast.info('系统错误',1,() => {
                    hhistory.goBack();
                })
            }
        })
    }
    render() {
        const { getFieldProps } = this.props.form
        return(
            <div>
                <InputItem
                    {...getFieldProps('username',{ initialValue:this.state.info.username,rules: [{required: true}] })}
                    clear
                    placeholder="必填"
                    autoFocus
                >美团外卖商家账号</InputItem>
                <InputItem
                    {...getFieldProps('password',{initialValue:this.state.info.password, rules: [{required: true}] })}
                    clear
                    placeholder="必填"
                    autoFocus
                >
                    美团外卖商家密码</InputItem>
                <Item
                    extra={<Switch
                        {...getFieldProps('riderFlg', {
                            initialValue: true,
                            valuePropName: 'checked',
                        })}
                        platform="ios"
                    />}
                >第三方骑手接单状态</Item>
            </div>
        )
    }
}

const mapStateToProps = (state) => ({

})
const mapDispatchToProps = (dispatch) => ({
    tochangeNav: navogj => dispatch(changeNav(navogj))
})

export default createForm()(connect(mapStateToProps, mapDispatchToProps)(WxmtPutInfo))